﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLibrary.BLL;
using System.Data.SqlClient;

namespace BusinessLibrary.DAL
{
    class sqlSpecialite
    {
        public static Specialite ReaderToObject(SqlDataReader dr)
        {
            Specialite _specialite = new Specialite();
            _specialite.Id = int.Parse(dr["Id"].ToString());

            _specialite.Nom = dr["Nom"].ToString();
            _specialite.Abreviation = dr["Abreviation"].ToString();



            if (!object.ReferenceEquals(dr["Id_departement"], DBNull.Value))
            {
                _specialite.departement = Departement.GetDepartementById(int.Parse(dr["Id_departement"].ToString()));
            }
            return _specialite;
        }

        public static Specialite GetSpecialiteById(int id)
        {
            Specialite specialite = new Specialite();
            string strSQL = @"select * from Specialite where UserId=@Id";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);
                    cmd.Parameters.AddWithValue("@Id", id);
                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        specialite = ReaderToObject(dr);
                    }
                    dr.Close();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return specialite;
        }

        public static int insertSpecialite(Specialite s)
        {
            int n = 0;
            string strSQL = @"insert into Specialite (Nom,Abreviation,Id_niveau) values(@nom, @abreviation, @id_niveau)";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);

                    cmd.Parameters.AddWithValue("@nom", s.Nom);
                    cmd.Parameters.AddWithValue("@abreviation", s.Nom);
                    cmd.Parameters.AddWithValue("@id_departement", s.departement.Id);

                    n = cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int updateSpecialite(Specialite s)
        {
            int n = 0;

            string strSQl = @"UPDATE Specialite SET (@nom IS NULL OR Nom=@nom), (@abreviation IS NULL OR Abreviation= @abreviation) Id_departement=@id_departement where Id=@id ";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();

                    SqlCommand cmd = new SqlCommand(strSQl, cnx);

                    cmd.Parameters.AddWithValue("@nom", s.Nom);
                    cmd.Parameters.AddWithValue("@abreviation", s.Abreviation);
                    cmd.Parameters.AddWithValue("@id_departement", s.departement.Id);
                    n = cmd.ExecuteNonQuery();

                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="idAuthentif"></param>
        /// <returns></returns>
        public static int deletSpecialite(int id, string nom, string abreviation, int iddepartement)
        {
            int n = 0;
            string strSQL = @"delete from Specialite where id=@id)";

            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);

                    cmd.Parameters.AddWithValue("@id", id);


                    n = cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }

        public static List<Specialite> SelectListeSpecialite(int id)
        {
            string strSQL = @"select * from Specialite where Id_niveau ="+id.ToString();
            using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
            {
                cnx.Open();

                SqlCommand cmd = new SqlCommand(strSQL, cnx);


                SqlDataReader dr = cmd.ExecuteReader();

                List<Specialite> List_specialite = new List<Specialite>();
                while (dr.Read())
                {
                    List_specialite.Add(ReaderToObject(dr));
                }

                dr.Close();
                cnx.Close();

                return List_specialite;
            }

        }

    }
}